function post(url, data) {
  return fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    body: JSON.stringify(data)
  })
  .then(response => {
    if (!response.ok) {
      throw new Error('Request failed with status: ' + response.status);
    }
    return response.text().then(text => text ? JSON.parse(text) : {});
  });
}

async function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function sleepWithAlarms(ms) {
  return new Promise((resolve, reject) => {
    const alarmName = `sleepAlarm_${Date.now()}`; // 使用当前时间戳创建一个唯一的定时器名称

    function onAlarm(alarm) {
      if (alarm.name === alarmName) {
        chrome.alarms.onAlarm.removeListener(onAlarm); // 清理监听器
        resolve();
      }
    }

    chrome.alarms.onAlarm.addListener(onAlarm);

    chrome.alarms.create(alarmName, { delayInMinutes: ms / 60000 });
  });
}

function showOverlay() {
  const overlay = document.createElement('div');
  overlay.style.cssText = 'position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(139, 147, 209, 0.5); display: flex; justify-content: center; align-items: center; z-index: 1000;';
  overlay.innerHTML = `<div style="color: red; font-size: 80px; font-weight: bold; line-height: 1.5;">脚本执行中<br/>请勿关闭此页面</div>`;
  document.body.appendChild(overlay);
}

async function switchCountry(counry) {
  let endpoint = await getEndpoint()
  let urls = {
    us: {
      'VM': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.ADMTRNLAQB6TYSWCFL6CJP73Q4DA&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.ABFRDKFRCGUV3457ZLVZSKKMMISA&ignore_selection_changed=true',
      'CB': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.ACUYR2QV5EVLECK2F3EW2CHIUWHA&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.ABOLDNDEZM3TNC6FDNOVWCTJGHYA&ignore_selection_changed=true',
      'DJ': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.ACSMZSQJIANH25JOMFK4MMML2P4Q&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.AA7OYZT4R7VBFRCEBCKBCW2TKCIA&ignore_selection_changed=true',
      'AD': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.AC7HKPYLRTO5XRT2FBM7QX3TXBNA&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.ACXNXESCDHS5WKAVUMAJYKQTAW3Q&ignore_selection_changed=true',
      'JBJ': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.ACRDN53YVDRIYOWSQPZWGTO4LI5A&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.AANW3HVM63N3JLDC5DJA3JAIXH6Q&ignore_selection_changed=true',
      'DL': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.AAGFPHK4EYWPJDS4WM2YZRIGRKJQ&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.ACKSUXX2YKPLHNWIQ66HLMM7BYNQ&ignore_selection_changed=true',
      'MA': 'https://sellercentral.amazon.com/home?mons_sel_dir_mcid=amzn1.merchant.d.AB5RVGSE7PF65KRVNAG227EF6WAQ&mons_sel_mkid=ATVPDKIKX0DER&mons_sel_dir_paid=amzn1.pa.d.AANNAOEHIRF4RQLKRDC3XOT3BZKQ&ignore_selection_changed=true'
    }
  }

  let url = urls[counry][endpoint]
  
  await openUrlAndExecScript(url, 'blank_content.js', msg => console.log(msg))
}
